package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.ymatou.iapi.livebuyer.parameter.QueryActivityProductsBean;
import com.ymatou.iapi.livebuyer.service.QueryActivityProductsCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveActivityProduct;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*查询直播间中的活动商品信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryActivityProducts{
	private static QueryActivityProductsBean queryactivityproductsBean;
	private static QueryActivityProductsCall queryactivityproductsCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询直播间中的活动商品信息");
	}

	@Before
	public void caseUp() {
		queryactivityproductsBean=new QueryActivityProductsBean();
		queryactivityproductsCall=new QueryActivityProductsCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryactivityproductsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryActivityProducts_001() {
		Logger.start(true,"查询直播间中的活动商品信息,成功");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			Date starttime2 = new Date(now.getTime() +  10 * 1000);
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime2);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			
			List<Integer> liveIdList=new ArrayList<Integer>();
			liveIdList.add(createlive.getId());
			queryactivityproductsBean.setLiveIdList(liveIdList);
			queryactivityproductsCall.setData(queryactivityproductsBean);
			queryactivityproductsCall.callService();			
			Logger.verifyEquals("200", queryactivityproductsCall.getString("code"), "验证code");
			JsonArray ja = queryactivityproductsCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(1, ja.size(), "验证数据");
			ja.forEach(data->{
				List<LiveActivityProduct> LiveActivityProduct = new ArrayList<>();
				try {
					LiveActivityProduct = liveRoomWapper.selectLiveactivityByProduct(productId);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(LiveActivityProduct.size()>0){
					Logger.verifyEquals(data.getAsJsonObject().get("liveId").getAsInt(),LiveActivityProduct.get(0).getLiveId(),"验证id");
					Logger.verifyEquals(data.getAsJsonObject().get("liveActivityId").getAsInt(),LiveActivityProduct.get(0).getLiveActivityId(),"验证LiveActivityId");
					String dbstarttime = YMTDateUtil.getFormatDateString(LiveActivityProduct.get(0).getStartTime(), "yyyy-MM-dd HH:mm:ss");
					String dbendtime = YMTDateUtil.getFormatDateString(LiveActivityProduct.get(0).getEndTime(), "yyyy-MM-dd HH:mm:ss");
					Logger.verifyEquals(data.getAsJsonObject().get("startTime").getAsString(),dbstarttime,"验证starttime");
					Logger.verifyEquals(data.getAsJsonObject().get("endTime").getAsString(),dbendtime,"验证endtime");

				}else{
					Logger.verifyNotNull(null, "没有查到秒杀数据:"+productId);
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryActivityProducts_002() {
		Logger.start(true,"查询多个直播间中的活动商品信息,成功");
		try {
			
			//添加第一个商品
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			Date starttime2 = new Date(now.getTime() +  10 * 1000);
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime2);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			
			
			//添加第二个商品
			sellerId=3383;
			Logger.comment("创建商品");
			addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
//			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			starttime = new Date(now.getTime() +  20 * 1000);
			endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive2 = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive2.getId(), sellerId, productId2);
			Logger.comment("查询商品规格");
//			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			 cids = CIWapper.selectCatalogsByProductId(productId2).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			now = YMTDateUtil.getDate();
			starttime2 = new Date(now.getTime() +  10 * 1000);
			createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive2.getId(), cids.get(0), 5d, 6, starttime2);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			
			List<Integer> liveIdList=new ArrayList<Integer>();
			liveIdList.add(createlive.getId());
			liveIdList.add(createlive2.getId());
			
			queryactivityproductsBean.setLiveIdList(liveIdList);
			queryactivityproductsCall.setData(queryactivityproductsBean);
			queryactivityproductsCall.callService();			
			Logger.verifyEquals("200", queryactivityproductsCall.getString("code"), "验证code");
			JsonArray ja = queryactivityproductsCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(2, ja.size(), "验证数据");
			ja.forEach(data->{
				List<LiveActivityProduct> LiveActivityProduct = new ArrayList<>();
				try {
					LiveActivityProduct = liveRoomWapper.selectLiveactivityByProduct(data.getAsJsonObject().get("productId").getAsString());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(LiveActivityProduct.size()>0){
					Logger.verifyEquals(data.getAsJsonObject().get("liveId").getAsInt(),LiveActivityProduct.get(0).getLiveId(),"验证id");
					Logger.verifyEquals(data.getAsJsonObject().get("liveActivityId").getAsInt(),LiveActivityProduct.get(0).getLiveActivityId(),"验证LiveActivityId");
					String dbstarttime = YMTDateUtil.getFormatDateString(LiveActivityProduct.get(0).getStartTime(), "yyyy-MM-dd HH:mm:ss");
					String dbendtime = YMTDateUtil.getFormatDateString(LiveActivityProduct.get(0).getEndTime(), "yyyy-MM-dd HH:mm:ss");
					Logger.verifyEquals(data.getAsJsonObject().get("startTime").getAsString(),dbstarttime,"验证starttime");
					Logger.verifyEquals(data.getAsJsonObject().get("endTime").getAsString(),dbendtime,"验证endtime");

				}else{
					Logger.verifyNotNull(null, "没有查到秒杀数据:"+productId);
				}
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryActivityProducts_003() {
		Logger.start(true,"不输入liveidlist失败");
		try {
			queryactivityproductsCall.setData(queryactivityproductsBean);
			queryactivityproductsCall.callService();			
			Logger.verifyEquals("100", queryactivityproductsCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}